package com.junlaninfo.shouxie;

import java.util.concurrent.locks.LockSupport;

/**
 * Created by 辉 on 2020/9/10.
 */
public class MyFutureTask<V> implements Runnable {
    private MyCallable<V> myCallable;
    private V result;
    private Thread cuThread;

    public MyFutureTask(MyCallable<V> myCallable) {
        this.myCallable = myCallable;
    }

    public void run() {
        result = myCallable.call();
        LockSupport.unpark(cuThread);
    }

    public V get() {
        if (result != null) {
            return result;
        }
        // 让当前的主线程变为阻塞状态
        cuThread = Thread.currentThread();
        LockSupport.park(cuThread);
        return result;
    }
}
